const { setGlobal } = require("@dgck81lnn/koishi-plugin-eval/lib/worker").internal
const moduleWhitelist = [
  "@dgck81lnn/jvozba",
  "@dgck81lnn/lnnzhyz2svg",
  "@dgck81lnn/uiua",
  "@js-temporal/polyfill",
  "@lnnbot-private/49.json",
  "@lnnbot-private/7.json",
  "@lnnbot-private/asaki-kana-value.json",
  "@lnnbot-private/block-distribution.json",
  "@lnnbot-private/gzmtr.json",
  "@lnnbot-private/kyujitai",
  "@lnnbot-private/namefreq.json",
  "@lnnbot-private/sept-origin.json",
  "@lnnbot-private/sept-stat-poss-6.json",
  "@lnnbot-private/sept-strategy-6.json",
  "@lnnbot-private/solar.json",
  "@lnnbot-private/sun.js",
  "@lnnbot-private/svg-to-sfd",
  "@lnnbot-private/unicode-blocks",
  "@lnnbot-private/unihan",
  "@lnnbot-private/unihan-simplify",
  "acorn",
  "adm-zip",
  "ansi_up",
  "css-tree",
  "js-yaml",
  "lodash",
  "node:assert",
  "node:crypto",
  "node:dgram",
  "node:dns",
  "node:events",
  "node:querystring",
  "node:stream",
  "node:stream/web",
  "node:string_decoder",
  "node:url",
  "node:util",
  "node:zlib",
  "pinyin-pro",
  "punycode/",
  "seedrandom",
  "terser",
  "tshet-uinh",
  "tshet-uinh-deriver-tools",
  "unicode-name",
  "whatlang-interpreter",
  "xdi8-transcriber",
  "yitizi",
]
for (const m of moduleWhitelist.filter(m => m.startsWith("node:"))) moduleWhitelist.push(m.slice(5))

const _require = require
setGlobal("require", function require(module) {
  if (module === "punycode") module = "punycode/"
  if (moduleWhitelist.includes(module)) return _require(module)
  throw new Error(`module '${module}' not found`)
})

setGlobal("AbortController", AbortController)
setGlobal("AbortSignal", AbortSignal)
setGlobal("Blob", Blob)
setGlobal("Crypto", Crypto)
setGlobal("CryptoKey", CryptoKey)
setGlobal("CustomEvent", CustomEvent)
setGlobal("Event", Event)
setGlobal("EventTarget", EventTarget)
setGlobal("File", File)
setGlobal("FormData", FormData)
setGlobal("Headers", Headers)
setGlobal("Response", Response)
setGlobal("Request", Request)
setGlobal("SubtleCrypto", SubtleCrypto)
setGlobal("URL", URL)
setGlobal("URLSearchParams", URLSearchParams)
setGlobal("WebSocket", WebSocket)

setGlobal("clearImmediate", clearImmediate)
setGlobal("clearTimeout", clearTimeout)
setGlobal("fetch", fetch)
setGlobal("setImmediate", setImmediate)
setGlobal("setTimeout", setTimeout)
setGlobal("structuredClone", structuredClone)
setGlobal("crypto", crypto)
for (const [k, v] of Object.entries(require("node:stream/web"))) setGlobal(k, v)
setGlobal("inspect", require("node:util").inspect)

setGlobal(Symbol.for("nodejs.util.inspect.custom"), () => "[Object [sandbox global]]")

setGlobal("Temporal", require("@js-temporal/polyfill").Temporal)
setGlobal("lodash", require("lodash"))
setGlobal("_", require("lodash"))

setGlobal("CJS", function CJS(code) { CJS.cache ??= new Map(); if (CJS.cache.has(code)) return CJS.cache.get(code); const exports = Object.create(null), module = { exports }; new Function("module", "exports", code)(module, exports); CJS.cache.set(code); return module.exports })
